package leesoft.hub.actions;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat; 
import java.util.Date; 

import leesoft.toolbox.ViewToolKit;

import com.sisuntech.cntmgr.*;

public class SessionContext {
	private final static String SESSIONCONTEXT_KEY="__SESSION_CONTEXT__";
	private Map requestMap=null;
	private HttpSession currentSession;
	private HashMap<String,Object> valueMap=new HashMap<String,Object>();
	
	
	public static final String KEY_TFORM_CELLCHECK="form_template_for_cellcheck";
	public static final String KEY_TFORM_APPLICATION="form_template_for_application";
	public static SessionContext getInstance(HttpSession session)
	{
		SessionContext ctx=(SessionContext)session.getAttribute(SESSIONCONTEXT_KEY);
		if(ctx==null){
			ctx=new SessionContext();
			session.setAttribute(SESSIONCONTEXT_KEY, ctx);
			ctx.currentSession=session;
		}
		return ctx;
	}
	public static SessionContext getInstance(HttpServletRequest request)
	{
		SessionContext ctx=getInstance(request.getSession());
		ctx.requestMap=request.getParameterMap();
		return ctx;
	}

	public String getParameterString(Object key){
		if(requestMap==null)
			return "";
		else{
			String[] values=(String[])requestMap.get(key);
			if(values==null)
				return "";
			else
				return values[0];
		}
	}
	public void put(String key,Object value){
		valueMap.put(key, value);
	}
	public Object get(String key){
		return valueMap.get(key);
	}
	public Object fetch(String key){
		Object returnObj=valueMap.get(key);
		valueMap.remove(key);
		return returnObj;
		
	}
	public HttpSession getCurrentSession(){
		return currentSession;
	}
	public void resetForm(String actionValue){
		if(actionValue.equals("update")){
			this.put(SessionContext.KEY_TFORM_CELLCHECK, null);
			this.put(SessionContext.KEY_TFORM_APPLICATION, null);
		}
	}
	public String tFormFieldValue(ContentBase content,String key){
		String value;
		Map tFormMap=(Map)this.get(SessionContext.KEY_TFORM_CELLCHECK);
		if(tFormMap==null)
			value=content.getNewValue(key);
		else{
			value=(String)tFormMap.get("curObject."+key);
			if(value==null||value.equals(""))
				value=content.getNewValue(key);
		}
		return value;
	}
	public String tFormDateFieldValue(ContentBase content,String key){
		String value;
		Map tFormMap=(Map)this.get(SessionContext.KEY_TFORM_CELLCHECK);
		if(tFormMap==null)
			value=content.getNewValue(key);
		else{
			value=(String)tFormMap.get("curObject."+key);
			if(value==null||value.equals(""))
				value=content.getNewValue(key);
		}
		if(value.length()>10)
			value=value.substring(0,10);
		return value;
	}
	public String tFormCheckedBoxValue(ContentBase content,String key){
		String value=tFormFieldValue(content,key);
		return ViewToolKit.setChecked(value);
	}
	public String tAFormFieldValue(ContentBase content,String key){
		Map tFormMap=(Map)this.get(SessionContext.KEY_TFORM_APPLICATION);
		if(tFormMap==null)
			return content.getNewValue(key);
		
		String value=(String)tFormMap.get("curObject."+key);
		if(value==null||value.equals(""))
			value=content.getNewValue(key);
		return value;
	}
	public String tAFormCheckedBoxValue(ContentBase content,String key){
		String value=tAFormFieldValue(content,key);
		return ViewToolKit.setChecked(value);
	}
	public String getADateFromFormIfEmptySetDefault(ContentBase content,String key){
		String value=tAFormFieldValue(content,key);
		if(!(value==null||value.equals("")))
			return value.substring(0,10);
		else{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			value=format.format((new Date()));
			return value;
		}
	}
	
	
	public String cInnerHTMLChkBox(ContentBase content,String key){
		return "name='curObject."+key+"' id='checkbox_"+key+"' value='1' "+tFormCheckedBoxValue(content,key);
	}
	public String getDateIfEmptySetDefault(ContentBase content,String key){
		String value=content.getNewValue(key);
		if(!(value==null||value.equals("")))
			return value;
		else{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			value=format.format((new Date()));
			return value;
		}
	}
}
